/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pingouin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hippolyte
 */
public class TServeurCoord extends Thread {

    public final String awake = new String();
    public Partie p;
    public static final int PORT = 8056;

    public TServeurCoord(Partie p) {
        this.p = p;
    }

    @Override
    public synchronized void run() {
        
        ServerSocket s = null;
        try {
            s = new ServerSocket(PORT);
        } catch (IOException ex) {
            Logger.getLogger(TServeurCoord.class.getName()).log(Level.SEVERE, null, ex);
        }

        while (true) {
            try {
                
                Socket soc = s.accept();

                if (Partie.ressources != null 
                        && Partie.ressources.JoueurEnCours != null 
                        && Partie.ressources.JoueurEnCours.address != null &&
                        Partie.ressources.JoueurEnCours.address.equals(soc.getInetAddress())) {

                    // Un BufferedReader permet de lire par ligne.
                    BufferedReader plec = new BufferedReader(
                            new InputStreamReader(soc.getInputStream()));


                    String str = plec.readLine();          // lecture du message
//                int id = Integer.parseInt(str.split(";")[0]);
                    int x = Integer.parseInt(str.split(";")[0]);
                    int y = Integer.parseInt(str.split(";")[1]);

                    System.out.println("action clic sur " + x + " ; " + y);

                    Partie.actionClic(x, y);
                    plec.close();

                }


                soc.close();
//                s.close();
            } catch (IOException ex) {
                Logger.getLogger(TServeurCoord.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(TServeurCoord.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
